package com.dream.demo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 李年林
 * @since 2021/8/25 17:59
 */
public class Test {

    public static void main(String[] args) {

        //这里的projectJson和customerJson就是你远程调用获取到的json串，直接传进去就可以了

        String projectJson = "{\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"FNUMBER\": \"CUST00212003\",\n" +
                "            \"FNAME\": \"远洋新干线A\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"action\": \"/k3interface/common/findProjectRelation\",\n" +
                "    \"ok\": true,\n" +
                "    \"msg\": \"success\",\n" +
                "    \"code\": 1\n" +
                "}";

        String customerJson = "{\"data\": [\n" +
                "     {\n" +
                "       \"FNUMBER\": \"CUST10918\", \n" +
                "       \"FNAME\": \"株洲中海地产有限公司\"\n" +
                "     }],\n" +
                "    \"action\": \"/k3interface/common/findCustomerRelation\",\n" +
                "    \"ok\": true,\n" +
                "    \"msg\": \"success\",\n" +
                "    \"code\": 1\n" +
                "}";
        //思路：首先json是一个对象应解析成JSONObject
        JSONObject project = JSONObject.parseObject(projectJson);
        //获取project中的data
        JSONArray projectData = project.getJSONArray("data");

        JSONObject customer = JSONObject.parseObject(customerJson);
        JSONArray customerData = customer.getJSONArray("data");
        //对两个JSONArray进行Dto封装
        List<K3TaskDto> k3TaskDtos = new ArrayList<>();

        for (int i = 0; i < projectData.size(); i++) {
            K3TaskDto k3TaskDto = new K3TaskDto();
            //获取到customerData这个JSONArray的某一条，其中某一条就是一个JSONObject
            JSONObject customerJsonObject = (JSONObject) customerData.get(i);
            k3TaskDto.setCustomerId(customerJsonObject.get("FNUMBER").toString());
            k3TaskDto.setCustomerName(customerJsonObject.get("FNAME").toString());

            //同理projectData也是一样处理
            JSONObject projectJsonObject =  (JSONObject)projectData.get(i);
            k3TaskDto.setProjectId(projectJsonObject.get("FNUMBER").toString());
            k3TaskDto.setProjectName(projectJsonObject.get("FNAME").toString());

            k3TaskDtos.add(k3TaskDto);
        }

        k3TaskDtos.forEach(System.out::println);


    }

}
